

export const getRadarOption = (type, subOption) => {
    let {legend, xAxisData, seriesData} = subOption;

    let indicator = [];
    let max = [];

    for (let i = 0; i < seriesData.length; i++) {
        if(i === 0){
            seriesData[i].data.forEach((item,index)=>{
                max[index] = item;
            })
        }else {
            seriesData[i].data.forEach((item,index)=> {
                max[index] = Math.max(max[index],item);
            })
        }
    }

    xAxisData.forEach((item,index)=>{

        let obj = {};
        obj.name = item;
        obj.max = max[index];
        indicator.push(obj);
    })

    let option = {
        tooltip: {},
        legend: {
            data: legend,
        },
        grid: {
            left : 100,
            right : 100
        },
        radar: {
            name : {
                formatter: function (value) {
                    let maxLength = 20;
                    if (value.length > maxLength) {
                        return value.substring(0, maxLength) + '...'
                    } else {
                        return value;
                    }
                },
                textStyle: {
                    color: '#fff',
                    backgroundColor: '#999',
                    borderRadius: 3,
                    padding: [3, 5]
                }
            },
            indicator: indicator,
        }
    };

    if (type === 'radar1') {
        let arr = [];
        seriesData.map((d,index) => {
            let obj = {};
            obj.value = [...d.data];
            obj.name = legend[index];
            arr.push(obj);
        })
        seriesData = [];
        let obj = {};
        obj.type = 'radar';
        obj.data = [...arr];
        seriesData.push(obj);
    }

    option.series = seriesData;
    return option;

};
export const getFunnelOption = (type, subOption) => {
    let {legend, xAxisData, seriesData} = subOption;
    let option = {
        legend: {
            data: legend,
            selectedMode : 'single',
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c}%"
        },
        calculable: true,
    };

    if (type === 'funnel1') {
        let arr = [];
        seriesData.map(d => {
            d.type = 'funnel';
            d.left = '10%';
            d.top = 60;
            d.bottom = 60;
            d.width = '80%';
            d.sort = 'descending';
            d.gap = 2;
            d.label = {
                normal: {
                    show: true,
                    position: 'inside',
                    formatter: function (value) {
                        let maxLength = 6;
                        if (value.name.length > maxLength) {
                            return value.name.substring(0, maxLength) + '...'
                        }
                    }
                },
                emphasis: {
                    textStyle: {
                        fontSize: 20
                    }
                }
            };
            d.labelLine = {
                normal: {
                    length: 10,
                    lineStyle: {
                        width: 1,
                        type: 'solid'
                    }
                }
            };
            d.itemStyle = {
                normal: {
                    borderColor: '#fff',
                        borderWidth: 1
                }
            };

            d.data.forEach((item,index)=>{
                let obj = {};
                obj.value = item;
                obj.name = xAxisData[index];
                arr.push(obj);
            })

            d.data = [...arr];
        })
    }

    option.series = seriesData;
    return option;
};

export const getPieOption = (type, subOption) => {
    let {legend, xAxisData, seriesData} = subOption;
    let option = {
        legend : {
            data : legend,
            selectedMode : 'single',
        },
        grid: {
            left : 100,
            right : 100
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },

    };

    if (type === 'pie1') {
        option.visualMap = {
            show: false,
            min: 1,
            max: 100,
            inRange: {
                colorLightness: [0, 1]
            }
        };
        seriesData.map(d => {
            let arr = [];
            d.type = 'pie';
            d.radius = '55%';
            d.center = ['50%','50%'];
            d.roseType = 'radius';

            d.data.forEach((item,index)=>{
                let obj = {};
                obj.value = item;
                obj.name = xAxisData[index];
                arr.push(obj);
            })

            d.data = [...arr].sort(function (a, b) { return a.value - b.value; });

            d.label = {
                normal: {
                    textStyle: {
                        color: '#999'
                    }
                }
            };
            d.labelLine = {
                normal: {
                    lineStyle: {
                        color: '#999'
                    },
                    smooth: 0.2,
                    length: 10,
                    length2: 20
                }
            };

            d.itemStyle = {
                normal: {
                    color: '#c23531',
                    shadowBlur: 200,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            };

            d.animationType = 'scale',
                d.animationEasing = 'elasticOut',
                d.animationDelay = function (idx) {
                    return Math.random() * 200;
                }

        });
    } else if (type === 'pie2') {
        let arr = [];
        seriesData.map(d => {
            d.type = 'pie';
            d.radius = ['50%','70%'];
            d.avoidLabelOverlap = false;
            d.label = {
                normal: {
                    show: false,
                    position: 'center',
                    formatter: function (value) {
                        let maxLength = 6;
                        if(value.name.length > maxLength){
                            return value.name.substring(0,maxLength) + '...'
                        }
                    }
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '20',
                        fontWeight: 'bold'
                    }
                }
            };
            d.labelLine = {
                normal: {
                    show: false
                }
            };

            d.data.forEach((item,index)=>{
                let obj = {};
                obj.value = item;
                obj.name = xAxisData[index];
                arr.push(obj);
            })

            d.data = [...arr];

        })

    } else if (type === 'pie3') {
        let arr = [];
        seriesData.map(d => {
            d.type = 'pie';
            d.radius = '65%';
            d.center = ['50%','50%'];
            d.label = {
                normal: {
                    formatter: function (value) {
                        let maxLength = 30;
                        if (value.name.length > maxLength) {
                            return value.name.substring(0, maxLength) + '...'
                        }
                    }
                },
            };
            d.itemStyle = {
                emphasis: {
                    shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }

            d.data.forEach((item,index)=>{
                let obj = {};
                obj.value = item;
                obj.name = xAxisData[index];
                arr.push(obj);
            })

            d.data = [...arr];
        })
    }

    option.series = seriesData;
    return option;

};

export const getBarOption = (type, subOption) => {

    let {legend, xAxisData, seriesData} = subOption;

    // xAxisData.unshift('总量')

    let option = {
            legend: {
                data: legend
            },
            grid: {
                left : 100,
                right : 100,
                bottom : 100
            },
            dataZoom: [
                {
                    show: true,
                    realtime: true,
                    start: 0,
                    end: 100
                },
                {
                    type: 'inside',
                    realtime: true,
                    start: 0,
                    end: 100
                }
            ],
            tooltip: {
                trigger: 'axis',
                axisPointer : {
                    type : 'shadow'
                }
            },
            toolbox: {
                left: '90',
                feature: {
                    dataZoom: {
                        yAxisIndex: 'none'
                    },
                    restore: {},
                },
                emphasis: {
                    iconStyle: {
                        borderWidth: '2'
                    },
                }
            },
            xAxis: {
                type: 'category',
                data: xAxisData,
                // axisLabel: {
                //     interval: 0,
                //     formatter:function(value,index) {
                //         let newParamsName = "";// 最终拼接成的字符串
                //         let paramsNameNumber = value.length;// 实际标签的个数
                //         let provideNumber = 6;// 每行能显示的字的个数
                //         let rowNumber = Math.ceil(paramsNameNumber / provideNumber);// 换行的话，需要显示几行，向上取整
                //         if (rowNumber > 3) {
                //             rowNumber = 3;
                //         }
                //         /**
                //          * 判断标签的个数是否大于规定的个数， 如果大于，则进行换行处理 如果不大于，即等于或小于，就返回原标签
                //          */
                //         // 条件等同于rowNumber>1
                //         if (paramsNameNumber > provideNumber) {
                //             /** 循环每一行,p表示行 */
                //             for (let p = 0; p < rowNumber; p++) {
                //                 let tempStr = "";// 表示每一次截取的字符串
                //                 let start = p * provideNumber;// 开始截取的位置
                //                 let end = start + provideNumber;// 结束截取的位置
                //                 // 此处特殊处理最后一行的索引值
                //                 if (p === rowNumber - 1) {
                //                     // 最后一次不换行
                //                     tempStr = value.substring(start, paramsNameNumber);
                //                     if(tempStr.length > provideNumber){
                //                         tempStr = tempStr.substring(0,provideNumber) + '...';
                //                     }
                //                 } else {
                //                     // 每一次拼接字符串并换行
                //                     tempStr = value.substring(start, end) + "\n";
                //                 }
                //                 newParamsName += tempStr;// 最终拼成的字符串
                //             }
                //
                //         } else {
                //             // 将旧标签的值赋给新标签
                //             newParamsName = value;
                //         }
                //         //将最终的字符串返回
                //         return newParamsName
                //     }
                // }
            },
            yAxis: {
                type: 'value'
            },
    };

    if (type === 'bar1') {
        seriesData.map(d => {
            d.type = 'bar';
        });
    } else if (type === 'bar2') {
        let sum = [];
        option.xAxis.data.unshift('总量');
        option.legend.selectedMode = 'single';

        seriesData.map((d,index) => {
            sum[index] = d.data.reduce((prev, curr, idx, arr) => {
                if(!prev){
                    prev = 0;
                }
                if(!curr){
                    curr = 0;
                }
                return addfloat(Number(prev),Number(curr));
            });
            d.data.unshift(sum[index]);
        })


        seriesData.map((item,index) => {

            let obj = {};
            let arr = [];

            item.type = 'bar';
            item.stack = item.name;
            item.label = {
                normal: {
                    show: true,
                    position: 'inside'
                }
            };

            obj.type = 'bar';
            obj.stack = `${item.name}`;
            obj.itemStyle = {
                normal: {
                    barBorderColor: 'rgba(0,0,0,0)',
                    color: 'rgba(0,0,0,0)'
                },
                emphasis: {
                    barBorderColor: 'rgba(0,0,0,0)',
                    color: 'rgba(0,0,0,0)'
                }
            };

            for (let i = 0; i < item.data.length; i++) {
                let value = 0;
                if(item.data[i]) {
                    value = Number(item.data[i])
                }
                arr[i] = sum[index] - value;
            }

            obj.data = [...arr];

            seriesData.push(obj);
        });
    } else if (type === 'bar4') {
        option = {};
        option = {
            legend: {
                data: legend
            },
            grid: {
                left : 120,
                right : 100,
            },
            dataZoom:  [
                {
                    show: true,
                    orient: 'vertical',
                    realtime: true,
                    start: 0,
                    end: 100
                },
                {
                    type: 'inside',
                    orient: 'vertical',
                    realtime: true,
                    start: 0,
                    end: 100
                }
            ],
            tooltip: {
                trigger: 'axis',
                axisPointer : {
                    type : 'shadow'
                }
            },
            xAxis: {
                type : 'value',
            },
            toolbox: {
                left: '90',
                feature: {
                    dataZoom: {
                        xAxisIndex: 'none'
                    },
                    restore: {},
                },
                emphasis: {
                    iconStyle: {
                        borderWidth: '2'
                    },
                }
            },
            yAxis: {
                type : 'category',
                data : xAxisData,
                axisLabel:{
                    margin: 30,
                    formatter: function(value,index){
                        return value.length>10?value.substr(0,7)+"...":value;
                    }
                },
            },
        };

        seriesData.map(d=>{
            d.type = 'bar';
            d.stack = '总量';
            d.label = {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            }
        })
    }

    option.series = seriesData;
    return option;

};

export const getLineOption = (type, subOption) => {

    let {legend, xAxisData, seriesData} = subOption;

    let option = {
        legend: {
            data: legend
        },
        grid: {
            left : 100,
            right : 100,
            bottom : 100
        },
        tooltip: {
            trigger: 'axis',
        },
        dataZoom: [
            {
                show: true,
                realtime: true,
                start: 0,
                end: 100
            },
            {
                type: 'inside',
                realtime: true,
                start: 0,
                end: 100
            }
        ],
        toolbox: {
            left: '90',
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                restore: {},
            },
            emphasis: {
                iconStyle: {
                    borderWidth: '2'
                },
            }
        },
        xAxis: {
            type: 'category',
            splitLine: {show: false},
            boundaryGap: false,
            data: xAxisData,
            // axisLabel: {
            //     interval: 0,
            //     formatter:function(value,index) {
            //         let newParamsName = "";// 最终拼接成的字符串
            //         let paramsNameNumber = value.length;// 实际标签的个数
            //         let provideNumber = 6;// 每行能显示的字的个数
            //         let rowNumber = Math.ceil(paramsNameNumber / provideNumber);// 换行的话，需要显示几行，向上取整
            //         if (rowNumber > 3) {
            //             rowNumber = 3;
            //         }
            //         /**
            //          * 判断标签的个数是否大于规定的个数， 如果大于，则进行换行处理 如果不大于，即等于或小于，就返回原标签
            //          */
            //         // 条件等同于rowNumber>1
            //         if (paramsNameNumber > provideNumber) {
            //             /** 循环每一行,p表示行 */
            //             for (let p = 0; p < rowNumber; p++) {
            //                 let tempStr = "";// 表示每一次截取的字符串
            //                 let start = p * provideNumber;// 开始截取的位置
            //                 let end = start + provideNumber;// 结束截取的位置
            //                 // 此处特殊处理最后一行的索引值
            //                 if (p === rowNumber - 1) {
            //                     // 最后一次不换行
            //                     tempStr = value.substring(start, paramsNameNumber);
            //                     if(tempStr.length > provideNumber){
            //                         tempStr = tempStr.substring(0,provideNumber) + '...';
            //                     }
            //                 } else {
            //                     // 每一次拼接字符串并换行
            //                     tempStr = value.substring(start, end) + "\n";
            //                 }
            //                 newParamsName += tempStr;// 最终拼成的字符串
            //             }
            //
            //         } else {
            //             // 将旧标签的值赋给新标签
            //             newParamsName = value;
            //         }
            //         //将最终的字符串返回
            //         return newParamsName
            //     }
            // }
        },
        yAxis: {
            type: 'value'
        },
    };

    if (type === 'line1') {
        seriesData.map(d => {
            d.type = 'line';
            d.smooth = false;
        });
    } else if (type === 'line2') {
        seriesData.map(d => {
            d.type = 'line';
            d.smooth = false;
            d.symbol = 'none';
            d.sampling = 'average';
            d.itemStyle = {
                normal: {
                    color: 'rgb(255, 70, 131)'
                }
            };
            d.areaStyle = {
                normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: 'rgb(255, 158, 68)'
                    }, {
                        offset: 1,
                        color: 'rgb(255, 70, 131)'
                    }])
                }
            };
        })
    } else if (type === 'line3') {

        let max = seriesData[0].data[0],min = seriesData[0].data[0];

        if(!max){
            max = '0';
        }
        if(!min){
            min = '0';
        }

        max = Number(max.replace(/,/g,''));
        min = Number(min.replace(/,/g,''));

        seriesData.forEach(d => {

            for ( let i = 0; i < d.data.length; i++){

                let temp = d.data[i];

                if (!temp) {
                    temp = '0';
                }

                temp = Number(temp.replace(/,/g,''));

                if (min > temp) {
                    min = temp;
                }
                if (max < temp) {
                    max = temp;
                }
            }

        })

        seriesData.map(d => {
            d.type = 'line';
            d.markLine = {
                silent: true,
                data: [{
                    yAxis: max * 0.2
                }, {
                    yAxis: max * 0.4
                }, {
                    yAxis: max * 0.6
                }, {
                    yAxis: max * 0.8
                }]
            };
        });

        option.visualMap = {
            top: 17,
            left: 'center',
            orient: 'horizontal',
            splitNumber: 5,
            min: min,
            max: max,
        };
    } else if (type === 'line4') {
        seriesData.map(d => {
            d.type = 'line';
            d.markLine = {
                data: [
                    {type: 'average', name: '平均值'}
                ]
            };
            d.markPoint = {
                data: [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ]
            };

        })
    }

    option.series = seriesData;
    return option;
};

const addfloat = function (arg1, arg2) {
    let r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2));
    return (arg1*m+arg2*m)/m
}